<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Admin\Mapper;

use App\Model\Channel;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 渠道列表Mapper类
 */
class ChannelMapper extends AbstractMapper
{
    /**
     * @var Channel
     */
    public $model;

    public function assignModel()
    {
        $this->model = Channel::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // ID
        if ( isset($params['id']) && filled($params['id']) ) {
            $query->where('id', '=', $params['id']);
        }
        
        // 名称
        if ( isset($params['name']) && filled($params['name']) ) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        return $query;
    }

    public function handlePageItems($items)
    {
        /** @var Channel $model */
        foreach ($items as $model) {
            $model->append(['url']);
        }
        return $items;
    }
}